package com.seleniti.enpareja.web.desktop.server.model.integration.service.impl.appengine;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.ioc.commons.utils.Format;
import org.ioc.commons.utils.jdo.JdoQueryHelper;
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
import org.joda.time.DateTimeZone;

import com.seleniti.enpareja.controller.common.exception.EnParejaException;
import com.seleniti.enpareja.controller.model.integration.database.domain.Couple;
import com.seleniti.enpareja.controller.model.integration.database.domain.WorkShift;
import com.seleniti.enpareja.controller.model.integration.database.domain.WorkShift.Shift;

public class GAEWorkShiftService extends GAEService {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(GAEWorkShiftService.class);

	public GAEWorkShiftService() {
		logger.info("GAE Couple service instance");
	}

	public WorkShift find(String id) {
		return pm.getObjectById(WorkShift.class, id);
	}

	public void delete(WorkShift ht) throws EnParejaException {
		pm.deletePersistent(ht);
	}

	public void deleteAll(Set<WorkShift> ht) throws EnParejaException {
		pm.deletePersistentAll(ht);
	}

	public void createOrReplace(Date date, Shift shiftMember1, Shift shiftMember2, String comment1, String comment2) {
		JdoQueryHelper qh = JdoQueryHelper.newQuery(pm, WorkShift.class);

		Couple couple = addLoggedUserCoupleFilter(qh);

		qh.andParams("date==%s", date);
		qh.setUnique(true);

		WorkShift found = (WorkShift) qh.execute();

		if (found == null) {
			found = new WorkShift();
			found.setCouple(couple);
			found.setDate(date);
		}

		found.setShiftMember1(shiftMember1);
		found.setShiftMember2(shiftMember2);
		found.setCommentsMember1(comment1);
		found.setCommentsMember2(comment2);

		pm.makePersistent(found);
	}

	public List<WorkShift> getList(Date from, Date to, boolean createIfMissing) {
		List<WorkShift> list = new ArrayList<WorkShift>();

		if (!createIfMissing) {
			JdoQueryHelper qh = JdoQueryHelper.newQuery(pm, WorkShift.class);
			addLoggedUserCoupleFilter(qh);

			qh.andParams("date >= %s && date <= %s", from, to);
			qh.setOrdering("date asc");

			@SuppressWarnings("unchecked")
			List<WorkShift> found = (List<WorkShift>) qh.execute();

			list.addAll(found);

		} else {
			DateTimeComparator dtc = DateTimeComparator.getDateOnlyInstance();
			DateTime current = new DateTime(from, DateTimeZone.UTC);
			while (dtc.compare(current, to) < 0) {

				Date date = current.toDate();
				JdoQueryHelper qh = JdoQueryHelper.newQuery(pm, WorkShift.class);
				Couple couple = addLoggedUserCoupleFilter(qh);

				qh.andParams("date == %s", date);
				qh.setUnique(true);

				WorkShift found = (WorkShift) qh.execute();

				if (found == null) {
					found = new WorkShift();
					found.setCouple(couple);
					found.setDate(date);
				}

				list.add(found);

				current = current.plusDays(1);

			}
		}
		
		logger.debug(Format.substitute("Dates got: {0}",Format.collectionToString(list, new Format.StringFormatter<WorkShift>() {

			@Override
			public String asString(WorkShift obj) {
				return obj!=null ? String.valueOf(obj.getDate()) : null;
			}
		})));

		return list;
	}

}
